//
//  JLTabBar.m
//  JL网易彩票progress
//
//  Created by sunjinliang on 15/11/6.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

#import "JLTabBar.h"
#import "JLTabBarButton.h"
@interface JLTabBar()
//当前选中的按钮
@property(nonatomic,strong)JLTabBarButton * curBtn;
@end
@implementation JLTabBar
//创建控制其对应的item
-(void)addTabBerButton:(UITabBarItem *)item{
        //1新建按钮
        JLTabBarButton * btn=[JLTabBarButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:btn];
        //2给按钮设置图片
        [btn setBackgroundImage:item.image forState:UIControlStateNormal];
        [btn setBackgroundImage:item.selectedImage forState:UIControlStateSelected];
        //3给按钮添加tag
//        btn.tag=i;
        //4 添加监听事件
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        
        if (self.subviews.count==1) {
            [self btnClick:btn];
        }

}
//通过xib和stroyboder创建View
    
    
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    
    if (self==[super initWithCoder:aDecoder]) {
        //[self setupButton];
    }
    
    return self;
}
//通过代码创建View
-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self==[super initWithFrame:frame]) {
        //[self setupButton];
    }
    return self;
}

//-(void)setupButton{
//    for (int i=0; i<5; i++) {
//    //1新建按钮
//    JLTabBarButton * btn=[JLTabBarButton buttonWithType:UIButtonTypeCustom];
//        [self addSubview:btn];
//    //2给按钮设置图片
//        UIImage * image=[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%d",i+1]];
//        UIImage * imageSel=[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%dSel",i+1]];
//        [btn setImage:image forState:UIControlStateNormal];
//        [btn setImage:imageSel forState:UIControlStateSelected];
//    //3给按钮添加tag
//        btn.tag=i;
//    //4 添加监听事件
//        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
//        if (i==0) {
//            [self btnClick:btn];
//        }
//        
//  }
//}
//按钮点击事件
-(void)btnClick:(JLTabBarButton *)btn{
    if ([self.delegate respondsToSelector:@selector(TabBar:Didselectedfrome:to:)]) {
        [self.delegate TabBar:self Didselectedfrome:self.curBtn.tag to:btn.tag];
    }
    self.curBtn.selected=NO;
    self.curBtn=btn;
    btn.selected=YES;
 

}

//3给按钮设置frame

-(void)layoutSubviews{
    NSInteger count=self.subviews.count;
    CGFloat btnH=self.bounds.size.height;
    CGFloat btnW=self.bounds.size.width*0.2;
    for (int i=0; i<count; i++) {
        UIButton * btn=self.subviews[i];
        btn.tag=i;
        btn.frame=CGRectMake(i*btnW, 0, btnW, btnH);
    }
    
}
















@end
